//
//  CourseDetailVideoCell.m
//  ShowYork
//
//  Created by hxh on 2020/9/2.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "LessonDetailVideoCell.h"
@interface LessonDetailVideoCell ()
@property (nonatomic,strong)UIImageView *stopVideoImgView;
@property (nonatomic,strong)UIImageView *coverImgView;
@property (nonatomic,strong)UILabel *titleLb;
@property (nonatomic,strong)UILabel *descLb;
@property (nonatomic,strong)UIImageView *headerIcon;
@property (nonatomic,strong)UIButton *confirmBtn;
@property (nonatomic,strong)CAGradientLayer *gradientLayer;
@end

@implementation LessonDetailVideoCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
    }
    return self;
}
- (void)setUI{
    self.coverImgView = [[UIImageView alloc]init];
    [self.contentView addSubview:self.coverImgView];
        
    [self.coverImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.contentView);
        make.height.mas_equalTo(kScreenW/16*9);
    }];
    
    self.confirmBtn = [[UIButton alloc]init];
    [self.confirmBtn setTitle:@"+加入学习" forState:UIControlStateNormal];
    [self.confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.confirmBtn.titleLabel.font = font(12);
    self.confirmBtn.layer.cornerRadius = 12.5;
    self.confirmBtn.layer.masksToBounds = YES;
    self.confirmBtn.backgroundColor = RGBA(204, 204, 204, 1);
    [self.confirmBtn addTarget:self action:@selector(confirmMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.confirmBtn];
    
    self.titleLb = [[UILabel alloc]init];
    self.titleLb.font = [UIFont boldSystemFontOfSize:18];
    self.titleLb.textColor = RGBA(35, 24, 24, 1);
    self.titleLb.numberOfLines = 0;
    [self.contentView addSubview:self.titleLb];
    
    [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(25);
        make.top.equalTo(self.titleLb);
        make.right.equalTo(self.contentView).offset(-16);
    }];

    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.coverImgView.mas_bottom).offset(24);
        make.left.equalTo(self.contentView).offset(16);
        make.right.equalTo(self.confirmBtn.mas_left).offset(-16);
    }];
    
    self.descLb = [[UILabel alloc]init];
    self.descLb.font = font(14);
    self.descLb.textColor = RGBA(102, 102, 102, 1);
    [self.contentView addSubview:self.descLb];
    [self.descLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLb.mas_bottom).offset(8);
        make.left.equalTo(self.contentView).offset(16);
        make.right.equalTo(self.contentView).offset(-16);
    }];
    
    self.stopVideoImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"video_stop"]];
    self.stopVideoImgView.userInteractionEnabled = YES;
    [self.contentView addSubview:self.stopVideoImgView];
    
    [self.stopVideoImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.coverImgView);
    }];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapMethod)];
    [self.stopVideoImgView addGestureRecognizer:tap];
    
    
    [self.confirmBtn layoutIfNeeded];
    
    self.gradientLayer =  [CAGradientLayer layer];
    self.gradientLayer.frame = self.confirmBtn.bounds;
    self.gradientLayer.endPoint = CGPointMake(0, 0);
    self.gradientLayer.startPoint  = CGPointMake(1.0, 0);
    self.gradientLayer.colors = @[(id)RGBA(119, 153, 255, 1).CGColor,(id)RGBA(86, 128, 250, 1).CGColor];
    
}

- (void)confirmMethod{
    if (self.delegate && [self.delegate respondsToSelector:@selector(addUserPlan)]) {
        [self.delegate addUserPlan];
    }
}

- (void)tapMethod{
    if (self.delegate && [self.delegate respondsToSelector:@selector(startPlayVideo)]) {
        [self.delegate startPlayVideo];
    }
}

- (void)setModel:(LessonPageItemModel *)model{
    _model = model;
    
    self.confirmBtn.userInteractionEnabled = !model.isSeleted;
//    self.confirmBtn.backgroundColor = model.isSeleted?RGBA(204, 204, 204, 1):[RPAppManager themColor];
    [self.confirmBtn setTitle:model.isSeleted?@"已加入学习":@"+加入学习" forState:UIControlStateNormal];
    [self.coverImgView sd_setImageWithURL:[NSURL URLWithString:model.cover]];
    self.titleLb.text = model.title;
    self.descLb.text = model.intro;
    
    
    CGFloat lessonTitleH = [RPAppManager getTextSizeWithText:model.title font:font(20) maxSize:CGSizeMake(kScreenW - 3 * 16 - 80 ,MAXFLOAT)].height;
    
    // 一排占位24高度左右
    if (lessonTitleH <= 24) {
        [self.confirmBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(80);
            make.height.mas_equalTo(25);
            make.centerY.equalTo(self.titleLb);
            make.right.equalTo(self.contentView).offset(-16);
        }];
    }else{
        [self.confirmBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(80);
            make.height.mas_equalTo(25);
            make.top.equalTo(self.titleLb);
            make.right.equalTo(self.contentView).offset(-16);
        }];
    }
    // 判断按钮状态是否需要添加渐变
    if (!model.isSeleted) {
        if (![self.confirmBtn.layer.sublayers containsObject:self.gradientLayer]) {
            [self.confirmBtn.layer insertSublayer:self.gradientLayer atIndex:0];
        }
    }else{
        [self.gradientLayer removeFromSuperlayer];
    }
}

@end
